package com.springboot.blog.controller.admin;

import com.springboot.blog.dto.LayPage;
import com.springboot.blog.dto.LayResponse;
import com.springboot.blog.entity.Blogroll;
import com.springboot.blog.interfaces.ToolInterface;
import com.springboot.blog.service.IBlogrollService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
class AdminBlogrollController {
    @Autowired
    IBlogrollService blogrollService;
    String PATH = "admin/blogroll/";

    @RequiresPermissions("blogroll:list")
    @GetMapping(value = {"/admin/blogroll/index", "/admin/blogroll"})
    public String index() {
        return PATH + "index";
    }

    @RequiresPermissions("blogroll:list")
    @GetMapping(value = {"/admin/blogroll/tree"})
    @ResponseBody
    public LayResponse tree(Integer page, Integer limit) {
        page = page == null ? 1 : page;
        limit = limit == null ? 10 : limit;
        LayPage<Blogroll> pageBlogroll = new LayPage<>(page, limit);
        blogrollService.page(pageBlogroll);
        return pageBlogroll.getLayResponse();
    }

    @RequiresPermissions("blogroll:delete")
    @PostMapping(value = {"/admin/blogroll/delete"})
    @ResponseBody
    public LayResponse delete(String id) {
        boolean result = blogrollService.removeById(id);
        LayResponse layResponse = new LayResponse();
        if (result) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("blogroll:delete")
    @PostMapping(value = {"/admin/blogroll/deleteAll"})
    @ResponseBody
    public LayResponse deleteAll(@RequestParam("ids[]") List<Integer> ids) {
        boolean result = blogrollService.removeByIds(ids);
        LayResponse layResponse = new LayResponse();
        if (result) {
            return layResponse.success();
        }
        return layResponse.error();
    }

    @RequiresPermissions("blogroll:add")
    @GetMapping(value = {"/admin/blogroll/add"})
    public String add(Model model) {
        return PATH + "add";
    }

    @RequiresPermissions("blogroll:add")
    @PostMapping(value = {"/admin/blogroll/add"})
    @ResponseBody
    public LayResponse add(@Validated(ToolInterface.insert.class) Blogroll blogroll) {
        blogrollService.save(blogroll);
        return new LayResponse().success();
    }

    @RequiresPermissions("blogroll:edit")
    @GetMapping(value = {"/admin/blogroll/edit"})
    public String edit(@RequestParam String id, Model model) {
        Blogroll data = blogrollService.getById(id);
        model.addAttribute("data", data);
        return PATH + "edit";
    }

    @RequiresPermissions("blogroll:edit")
    @PostMapping(value = {"/admin/blogroll/edit"})
    @ResponseBody
    public LayResponse edit(@Validated(ToolInterface.update.class) Blogroll blogroll) {
        blogrollService.updateById(blogroll);
        return new LayResponse().success();
    }

    @RequiresPermissions("blogroll:edit")
    @PostMapping(value = {"/admin/blogroll/ajax/{id}"})
    @ResponseBody
    public LayResponse ajax(Blogroll blogroll) {
        boolean b = blogrollService.updateById(blogroll);
        LayResponse layResponse = new LayResponse();
        if (b) {
            return layResponse.success();
        }
        return layResponse.error();
    }
}
